﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure;
using B.S.BaseData.Read.API.Application.Command;
using B.S.BaseData.Read.API.Dtos;
using MediatR;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace B.S.BaseData.Read.API.Application.CommandHandler
{
    /// <summary>
    /// 商品信息查询命令处理
    /// </summary>
    public class GoodsInfoQueryCommandHandler : IRequestHandler<GoodsInfoQueryCommand,ApiResult<ApiPaging<GoodsDTO>>>
    {
        /// <summary>
        /// 商品名称仓储
        /// </summary>
        private readonly IBaseRepository<GoodsType> goodstypeRepository;

        /// <summary>
        /// 商品信息仓储
        /// </summary>
        private readonly IBaseRepository<GoodsInfo> goodsInfoRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="goodstypeRepository">商品名称仓储</param>
        /// <param name="goodsInfoRepository">商品信息仓储</param>
        /// <param name="mapper">映射</param>
        public GoodsInfoQueryCommandHandler(IBaseRepository<GoodsType> goodstypeRepository, IBaseRepository<GoodsInfo> goodsInfoRepository, IMapper mapper)
        {
            this.goodstypeRepository = goodstypeRepository;
            this.goodsInfoRepository = goodsInfoRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>   
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiResult<ApiPaging<GoodsDTO>>> Handle(GoodsInfoQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<GoodsDTO>> result = new ApiResult<ApiPaging<GoodsDTO>>();
            result.Code = ApiEnums.查询成功;
            result.Msg = ApiEnums.查询成功.ToString();
            var linq = from a in goodstypeRepository.GetAll()
                       join b in goodsInfoRepository.GetAll().Where(x=>x.IsDelete == false) on a.GTid equals b.Gid
                       select new GoodsDTO
                       {
                           GTid = a.GTid,
                           GTName = a.GTName,
                           Gid = b.Gid,
                           GoodsId = b.GoodsId,
                           Production = b.Production,
                           Expiration = b.Expiration,
                           ProductionNumber = b.ProductionNumber,
                           ProductionHome = b.ProductionHome,
                           Img = b.Img,
                           CreateName = b.CreateName,
                           CreateTime = b.CreateTime,
                           UpdateName = b.UpdateName,
                       };
            if (!string.IsNullOrEmpty(request.Production))
            {
                linq = linq.Where(x => x.Production == request.Production);
            }
            if(request.Gid != 0)
            {
                linq = linq.Where(x => x.Gid == request.Gid);
            }
            var count = linq.Count();
            var page = (int)Math.Ceiling(count * 1.0 / request.PageSize);
            var models = linq.OrderByDescending(x => x.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            ApiPaging<GoodsDTO> paging = new ApiPaging<GoodsDTO>();
            paging.TotalPage = page;
            paging.TotalCount = count;
            paging.List = linq.ToList();
            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
